package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.MenuDescription;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 */
public class MenuDescipetinActivity extends Activity {
    private WebView MenuwebView;
    private TextView foodDescripiton, desptionFood, dkeywords, desption_name;
    private NetworkImageView desption_img;
    private String name;
    private long id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mun_descption);
        initView();
        getIntentData();
        initData();


    }


    public void getIntentData() {

        id = getIntent().getLongExtra("id", 1126);

    }

    private void initView() {

        MenuwebView = (WebView) findViewById(R.id.MenuwebView);
        foodDescripiton = (TextView) findViewById(R.id.foodDescripiton);
        desptionFood = (TextView) findViewById(R.id.desptionFood);
        dkeywords = (TextView) findViewById(R.id.dkeywords);
        desption_name = (TextView) findViewById(R.id.desption_name);
        desption_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        desption_img = (NetworkImageView) findViewById(R.id.desption_img);
    }

    private void initData() {
        final CookProgressDialog anmition = new CookProgressDialog(this, R.id.animaImg);
        anmition.show();
        String httpUrl = "http://apis.baidu.com/tngou/cook/show";
        String httpArg = "id=" + id;
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                Gson gson = new Gson();
                MenuDescription description = gson.fromJson(response, MenuDescription.class);
                if (description != null) {
                    foodDescripiton.setText(description.getDescription());
                    desptionFood.setText(description.getFood());
                    dkeywords.setText(description.getKeywords());
                    desption_name.setText(description.getName());

                    String url = " http://tnfs.tngou.net/image/" + description.getImg();
                    getBitmap(url, desption_img);

                    String htmlString = description.getMessage();
                    final String mimrTypeString = "text/html";
                    final String encoding = "utf-8";
                    MenuwebView.loadDataWithBaseURL("fake://not/needed", htmlString, mimrTypeString, encoding, "");
                    MenuwebView.setInitialScale(100);
                    anmition.dismiss();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {

            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };
        VolleySington.getInstance().addRequest(request);
    }

    private void getBitmap(String url, final NetworkImageView networkImageView) {
        ImageLoader imageLoader = VolleySington.getInstance().getImageLoader();
        networkImageView.setDefaultImageResId(R.mipmap.ic_launcher);
        networkImageView.setErrorImageResId(R.mipmap.ic_launcher);
        networkImageView.setImageUrl(url, imageLoader);


    }


}
